<template>
  <div class="login">
    <el-form class="form" :model="loginForm" :rules="rules" ref="loginFormRef">
      <div class="image"><img src="../assets/logo.png" /></div>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          prefix-icon="el-icon-user"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          prefix-icon="el-icon-lock"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item class="button">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="resetLoginForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度不能低于2位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 10,
            message: '长度不能低于6位,最多不能超过10位',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        const { data: user } = await this.$http.post('login', this.loginForm)
        if (user.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', user.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  .form {
    width: 500px;
    height: 300px;
    background: #eeeeee;
    position: relative;
    top: 30%;
    left: 30%;
    border-radius: 10px;
    position: absolute;
    padding: 0 10px;
    box-sizing: border-box;
    .image {
      width: 80px;
      height: 80px;
      border: solid 10px #ccc;
      border-radius: 100px;
      overflow: hidden;
      position: relative;
      top: -40px;
      left: 200px;
      background: #eee;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .button {
      display: flex;
      justify-content: flex-end;
    }
  }

  width: 100%;
  height: 100%;
  background: #2b4b6b;
}
</style>
